/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.enforcement.integral.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.validate.group.AuditGroup;
import com.hopes.bizz.enforcement.common.constant.IntegralDicConstant;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralExchangeAuditDTO;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralExchangeDTO;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralFlowSearchDto;
import com.hopes.bizz.enforcement.integral.entity.RiskIntegralExchangeEntity;
import com.hopes.bizz.enforcement.integral.service.RiskIntegralExchangeService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 兑换记录
 *
 * @author daijie
 * @date 2023-07-26 11:07:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskIntegralExchange")
@Tag(description = "riskIntegralExchange", name = "兑换记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskIntegralExchangeController {

    private final RiskIntegralExchangeService riskIntegralExchangeService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param integralExchangeDTO 兑换记录
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('integral_exchange_view')")
    public R getRiskIntegralExchangePage(Page page, RiskIntegralExchangeDTO integralExchangeDTO) {
        return R.ok(riskIntegralExchangeService.listPage(page, integralExchangeDTO));
    }

    @Operation(summary = "我的分页查询", description = "我的分页查询")
    @GetMapping("/myPage")
    @PreAuthorize("@pms.hasPermission('integral_exchange_view')")
    public R getMyRiskIntegralExchangePage(Page page, RiskIntegralExchangeDTO integralExchangeDTO) {
        return R.ok(riskIntegralExchangeService.listMyPage(page, integralExchangeDTO));
    }

    @Operation(summary = "查询我的待审批", description = "查询我的待审批")
    @GetMapping("/queryTodoForPage")
    @PreAuthorize("@pms.hasPermission('integral_exchange_view')")
    public R queryTodoForPage(Page page, RiskIntegralFlowSearchDto flowSearch) {
        flowSearch.setTaskKey(IntegralDicConstant.INTEGRAL_GOODS_AUDIT_STATUS_UNDER_REVIEW.toString());
        Page res = riskIntegralExchangeService.queryTodoForPage(page, flowSearch);
        return R.ok(res);
    }

    @Operation(summary = "查询我的已审批", description = "查询我的已审批")
    @GetMapping("/queryFinishForPage")
    @PreAuthorize("@pms.hasPermission('integral_exchange_view')")
    public R queryFinishForPage(Page page, RiskIntegralFlowSearchDto flowSearch) {
        flowSearch.setTaskKey(IntegralDicConstant.INTEGRAL_GOODS_AUDIT_STATUS_UNDER_REVIEW.toString());
        Page res = riskIntegralExchangeService.queryFinishForPage(page,flowSearch);
        return R.ok(res);
    }

    /**
     * 通过id查询兑换记录
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('integral_exchange_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(riskIntegralExchangeService.getById(id));
    }

    /**
     * 新增兑换记录
     *
     * @param riskIntegralExchange 兑换记录
     * @return R
     */
    @Operation(summary = "新增兑换记录", description = "新增兑换记录")
    @SysLog("新增兑换记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('integral_exchange_add')")
    public R save(@RequestBody RiskIntegralExchangeEntity riskIntegralExchange) {
        return R.ok(riskIntegralExchangeService.save(riskIntegralExchange));
    }

    /**
     * 修改兑换记录
     *
     * @param riskIntegralExchange 兑换记录
     * @return R
     */
    @Operation(summary = "修改兑换记录", description = "修改兑换记录")
    @SysLog("修改兑换记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('integral_exchange_edit')")
    public R updateById(@RequestBody RiskIntegralExchangeEntity riskIntegralExchange) {
        return R.ok(riskIntegralExchangeService.updateById(riskIntegralExchange));
    }

    /**
     * 通过id删除兑换记录
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除兑换记录", description = "通过id删除兑换记录")
    @SysLog("通过id删除兑换记录")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('integral_exchange_del')")
    public R removeById(@RequestBody Long[] ids) {
        riskIntegralExchangeService.deleteTask(CollUtil.toList(ids));
        return R.ok();
    }


    /**
     * 导出excel 表格
     *
     * @param riskIntegralExchange 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('integral_exchange_export')")
    public List<RiskIntegralExchangeEntity> export(RiskIntegralExchangeEntity riskIntegralExchange) {
        return riskIntegralExchangeService.list(Wrappers.query(riskIntegralExchange));
    }


    @Operation(summary = "提交积分兑换审核", description = "提交积分兑换审核")
    @SysLog("提交积分兑换审核")
    @PostMapping("/handlerAudit")
    public R handlerAudit(@Validated(AuditGroup.class) @RequestBody RiskIntegralExchangeAuditDTO riskIntegralExchangeAuditDTO) {
        riskIntegralExchangeService.handlerAudit(riskIntegralExchangeAuditDTO);
        return R.ok();
    }
}
